local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    Asset( "IMAGE", "images/map_icons/npc_item_treasure_map.tex" ),  -- 背包贴图
    Asset( "ATLAS", "images/map_icons/npc_item_treasure_map.xml" ),
}
local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_event_wurt_hiding_box"] or {}
end

local function add_container_before_not_ismastersim_retrun(inst)
	-------------------------------------------------------------------------------------------------
    ------ 添加背包container组件    --- 必须在 SetPristine 之后，
    local container_WidgetSetup = "wobysmall"
    if TheWorld.ismastersim then
        inst:AddComponent("container")
        inst.components.container.openlimit = 1  ---- 限制1个人打开
       inst.components.container:WidgetSetup(container_WidgetSetup)
        -- container_Widget_change(inst.components.container)
            -- inst.components.container.numslots = 9 -- 格子数  (不能用这个，会造成bug)   
    else
        inst:DoTaskInTime(0.1, function()
            if inst.replica then
                if inst.replica.container then
                   inst.replica.container:WidgetSetup(container_WidgetSetup)
                -- container_Widget_change(inst.replica.container)
                end
            end

        end)
    end
    -------------------------------------------------------------------------------------------------
end
-----------------------------------------------------------------------------------------------
local function Check_player_unlock_Wurt(theplayer)
    if theplayer and theplayer.userid and theplayer.components.playercontroller then    ----- 必须是玩家
        local dlc_charaters = theplayer.npc_base_lib:___Get_DLC_Character_List() or {}
        if dlc_charaters["wurt"] == true then
            ---------------------------------------------------------------
            --- 该玩家解锁了小鱼人
            return true
            ---------------------------------------------------------------
        end
    end
    return false
end
local function near_player_check(inst)
    local theplayer = inst:GetNearestPlayer(true)
    local pt = Vector3(inst.Transform:GetWorldPosition())
    if theplayer and inst.npc_base_lib:Distance_Targets(inst,theplayer) < 40 and Check_player_unlock_Wurt(theplayer) == true then
        inst:Show()
        inst:RemoveTag("INLIMBO")
        inst.Transform:SetPosition(pt.x, 0, pt.z)

    else
        inst:Hide()
        inst:AddTag("INLIMBO")
        inst.Transform:SetPosition(pt.x, 20, pt.z)
    end
end

local function On_Open(inst,doer)
    if Check_player_unlock_Wurt(doer) == true then
        local x,y,z = inst.Transform:GetWorldPosition()
        local npc = SpawnPrefab("wurt_npc")
        npc.__link_player = doer
        npc:Hide()
        npc:DoTaskInTime(0.1,function()
            npc.Transform:SetPosition(x, y, z)
            npc:Show()
            npc:AddDebuff("npc_event_wurt_hiding_box", "npc_event_wurt_hiding_box")
        end)

    end
end
-----------------------------------------------------------------------------------------------
local function fn()
    TheSim:LoadPrefabs({ "quagmire_crate" })    ----- 暴食里的物品都得用这个加载素材

    local inst = CreateEntity()

    inst.entity:AddTransform()
	inst.entity:AddNetwork()
	inst.entity:AddMiniMapEntity()
    inst.entity:AddAnimState()

    inst.AnimState:SetBank("quagmire_crate")
    inst.AnimState:SetBuild("quagmire_crate")
    inst.AnimState:PlayAnimation("idle",false)

    inst:AddTag("npc_item_wurt_hiding_box")
    inst:AddTag("INLIMBO")
    inst:Hide()

    MakeObstaclePhysics(inst, .75)

    inst.entity:SetPristine()
    -- inst:DoTaskInTime(0,inst.Hide)
    add_container_before_not_ismastersim_retrun(inst)
    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")


    inst:AddComponent("inspectable") --可检查组件   
    inst:AddComponent("named")
    -- inst.components.named:SetName(tostring(_table.Name))
    inst.components.named:SetName(GetStringsTable().box_name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_wurt_hiding_box")] = GetStringsTable().inspect_str_box  --人物检查的描述
    -----------------------------------------------------------------------------------------------------------------------------------------------
   
    inst:ListenForEvent("onopen",function(inst,_table)
        inst.components.container:Close()
        if _table and _table.doer and inst.__canbeopened ~= true then
            On_Open(inst,_table.doer)
            inst.__canbeopened = true
            inst.components.container.canbeopened = false
        end

    end)


    inst:ListenForEvent("onclose",function()
        inst.components.container:DropEverything()
        inst.AnimState:PlayAnimation("unwrap")
        inst:ListenForEvent("animover",inst.Remove)
    end)
    -----------------------------------------------------------------------------------------------------------------------------------------------
    inst:DoTaskInTime(0.1,function()
        local pt = Vector3(inst.Transform:GetWorldPosition())
        inst.Transform:SetPosition(pt.x, 20, pt.z)
    end)
    inst:DoPeriodicTask(5,near_player_check)
    -- inst.Transform.GetWorldPosition
    -----------------------------------------------------------------------------------------------------------------------------------------------
    return inst
end

return Prefab("npc_item_wurt_hiding_box", fn,assets)